package com.qxmly.fm;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qxmly.fm.adapters.DetailAdapter;
import com.qxmly.fm.interfaces.IDetailCallBack;
import com.qxmly.fm.presenters.DetailPresenter;
import com.qxmly.fm.presenters.PlayerPresenter;
import com.qxmly.fm.utils.ImageBlur;
import com.qxmly.fm.utils.LogUtil;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.xmlywind.sdk.videoAd.BaseAdActivity;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.List;

/**
 * Q
 * Date:2021/11/28$
 **/
public class DetailActivity extends BaseAdActivity implements IDetailCallBack, DetailAdapter.OnItemClickListener {
    private ImageView mLargeCover;
    private ImageView mMiddleCover;
    private TextView mDetailTitle;
    private TextView mDetailAuthor;
    private DetailPresenter mDetailPresenter;
    private int mCurrentPage = 1;
    private String TAG = "DetailActivity";
    private RecyclerView mDetailList;
    private DetailAdapter mDetailAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        //透明状态栏
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        initView();
        mDetailPresenter =DetailPresenter.getInstance();
        mDetailPresenter.registerCallBack(this);

    }
    private void initView() {
        mLargeCover = this.findViewById(R.id.detail_lager_cover);
        mMiddleCover = this.findViewById(R.id.detail_middle_cover);
        mDetailTitle = this.findViewById(R.id.detail_title);
        mDetailAuthor = this.findViewById(R.id.detail_author);
        mDetailList =this.findViewById(R.id.detail_list);
        //设置布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mDetailList.setLayoutManager(layoutManager);
        //设置适配器
        mDetailAdapter = new DetailAdapter();
        mDetailList.setAdapter(mDetailAdapter);
        mDetailList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                //super.getItemOffsets(outRect, view, parent, state);
                //dp转像素,设置间距
                outRect.top= UIUtil.dip2px(view.getContext(),2);
                outRect.bottom=UIUtil.dip2px(view.getContext(),2);
                outRect.left=UIUtil.dip2px(view.getContext(), 2);
                outRect.right=UIUtil.dip2px(view.getContext(), 2);
            }
        });

        mDetailAdapter.setOnItemClickListener(this);
    }


    @Override
    public void onDetailListLoader(List<Track> tracks) {
        mDetailAdapter.setData(tracks);


    }

    @Override
    public void onAlbumLoader(Album album) {
        //获取专辑详情
        long id = album.getId();
        LogUtil.d(TAG,"album->"+id);
        mDetailPresenter.getAlbumDetail((int) id,mCurrentPage);
        if(mDetailTitle!=null){
            //LogUtil.d(TAG,"success title");
            mDetailTitle.setText(album.getAlbumTitle());
        }
        if(mDetailAuthor!=null){
            mDetailAuthor.setText(album.getAnnouncer().getNickname());
        }
        if (mLargeCover != null) {
            Picasso.with(this).load(album.getCoverUrlLarge()).into(mLargeCover, new Callback() {
                @Override
                public void onSuccess() {
                    Drawable drawable = mLargeCover.getDrawable();
                    if (drawable != null) {
                        ImageBlur.makeBlur(mLargeCover, DetailActivity.this);
                    }
                }

                @Override
                public void onError() {
                    LogUtil.e(TAG,"mLargeCover ERROR");

                }
            });
        }
        if(mMiddleCover!=null){
            Picasso.with(this).load(album.getCoverUrlLarge()).into(mMiddleCover);
        }
    }

    @Override
    public void onItemClick(List<Track> detailData, int position) {
        PlayerPresenter playerPresenter = PlayerPresenter.getPlayerPresenter();
        playerPresenter.setPlayList(detailData,position);
        Intent intent = new Intent(this,PlayActivity.class);
        startActivity(intent);

    }
}
